﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace working
{
    public partial class GetForm : Form
    {
        private WorkItem workItem;
        public Action<WorkItem> onApply { get; set; }
        public GetForm()
        {
            InitializeComponent();
            branch.TextChanged += OnModifyBranch;
        }

        private void OnModifyBranch(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(workItem.fileID)){
                textBox_path.Text = branch.Text;
            }
        }

        public void Show(WorkItem workItem)
        {
            this.workItem = workItem;
            if(this.workItem == null){
                this.workItem = new WorkItem();
            }
            ReLoadWorkItemInfo();
            this.Show();
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            base.OnClosing(e);
            Hide();
        }

        private void ReLoadWorkItemInfo()
        {
            title.Text = workItem.name;
            branch.Text = workItem.id;
            timepicker.Value = workItem.time;
            taginfos.Text = workItem.tags;
            decription.Text = workItem.description;
            web.Text = workItem.web;
            source.Text = workItem.source;
            textBox_path.Text = workItem.fileID;
            if (string.IsNullOrEmpty(workItem.fileID)){
                textBox_path.Text = branch.Text;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            workItem.name = title.Text;
            workItem.id = branch.Text;
            workItem.time = timepicker.Value;
            workItem.tags = taginfos.Text;
            workItem.description = decription.Text;
            workItem.web=web.Text;
            workItem.source = source.Text;
            workItem.fileID = textBox_path.Text;
            if (string.IsNullOrEmpty(workItem.fileID)){
                workItem.fileID = workItem.id;
            }

            if (onApply != null)
            {
                onApply.Invoke(workItem);
            }

            SetVisibleCore(false);
        }
    }
}
